package br.unifor.cct.mia.evaluate.pso;

import br.unifor.cct.mia.evaluate.Evaluate;
import br.unifor.cct.mia.evaluate.InvalidLearnerTypeException;
import br.unifor.cct.mia.evolutionary.Particula;
import br.unifor.cct.mia.pso.PSO;

public class PSOSomatorioEvaluate implements Evaluate {

	public Object evaluate(Object o, String[] options)
			throws InvalidLearnerTypeException {
		Object[] value = (Object[])o;
		PSO pso = (PSO)value[0];
		
		for (Particula p:pso.getPopulacao()) {
			double fitness = evaluate(p);
			
			p.setFitness(fitness);
			if ( fitness>p.getPBest().getFitness() ) {
				p.setPBest((Particula)p.clone());
			}
			
			if ( fitness>pso.getBestIndividual().getFitness() ) {
				pso.setBestIndividual((Particula)p.clone());
			}
		}
		return pso.getPopulacao();
	}
	
	private double evaluate(Particula p) {
		double result = 0;
		for( double x:p.getPosition() ) {
			result += x;
		}
		return result;
	}	

}
